package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.Camera.Area;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.adapter.ChinaCityAdapter;
import com.itboye.textileexpes.beans.CityBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.ListToStringUtil;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textillexepres.db.AddreaCitybeans;
import com.itboye.textillexepres.db.DBManager;

/**
 * @Area 中国区县一级
 * @author
 * 
 */
public class ActivityAreaChina extends BaseActivity implements OnClickListener {
	private BaseAdapter adapter;
	private ListView country_lv_list;
	private Handler handler;
	private SQLiteDatabase database;
	private ArrayList<CityBeans> mCityNames;
	private ImageView back;
	EditText edit_inputcity;
	private DBManager dbManager;
	public String city;
	private String province;
	private String area;
	String type;// 1、直辖市 2、城市
	boolean searchType = true;// true、根据 城市搜索店铺 false、根据地区搜索店铺
	RelativeLayout relypoutGps;
	String countryId;
	CityBeans cityModel;
	String provinceID;
	private ImageView img_other;
	private TextView title;
	IsMobileNumPhone numPhone;
	String CountryCityArea;
	String CoutrtID;

	String cuntryNmae;
	String provinceNmae;
	String cityNmae;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_city;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		country_lv_list = (ListView) findViewById(R.id.country_lv_list);
		provinceID = getIntent().getStringExtra("id");
		CoutrtID = getIntent().getStringExtra("provinceID");
		cuntryNmae = getIntent().getStringExtra("countryNmae");
		cityNmae = getIntent().getStringExtra("cityName");
		// 国家ID
		province = getIntent().getStringExtra("province");
		city = getIntent().getStringExtra("city");
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		cityModel = new CityBeans();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);

		handler = new Handler();
		mCityNames = getCityNames();

		img_other.setVisibility(View.GONE);
		title.setText("我的地址");
		setAdapter(mCityNames);

		country_lv_list.setOnItemClickListener(new CityListOnItemClick());
	}

	/**
	 * 从数据库获取城市数据
	 * 
	 * @return
	 */
	private ArrayList<CityBeans> getCityNames() {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_area where father=" + provinceID
						+ " ORDER BY area", null);
		System.out.println("cursor" + cursor);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			CityBeans cityModel = new CityBeans();
			cityModel.setProvince(cursor.getString(cursor
					.getColumnIndex("area")));

			cityModel.setId(cursor.getString(cursor.getColumnIndex("father")));
			names.add(cityModel);

		}
		return names;
	}

	/**
	 * 城市列表点击事件
	 * 
	 * @author sy
	 * 
	 */
	class CityListOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {

			searchType = true;

			cityModel = (CityBeans) country_lv_list.getAdapter().getItem(pos);
			area = cityModel.getProvince();
			String name = province + area;
			String citynameArea = city + area;
			if (province == null) {
				onProvinceCityArea(citynameArea);
				finish();
				return;
			} else {
				onProvinceCityArea(name);
				finish();
			}

		}

	}

	/**
	 * 发送广播返回城市区域
	 * 
	 * @param name
	 */
	public void onProvinceCityArea(String name) {

		Intent intent3 = new Intent("AddressBeans");
		sendBroadcast(intent3);

		SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.AddressBeans,
				name + "");
		SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.AREAID,
				cityModel.getId() + "");

		SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.Area, area
				+ "");
		System.out.println("DOUBI" + area);
		SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.TilteCity,
				city + "");
		SPUtils.put(ActivityAreaChina.this, null,
				SpPublicParams.CountryCityArea, province + "");

		SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.COUTRYID, "1");
		SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.PROVINCEID,
				CoutrtID + "");
		SPUtils.put(ActivityAreaChina.this, null,
				SpPublicParams.PROVINCECITYID, provinceID + "");
		SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.AREAID,
				cityModel.getId() + "");

		SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.COUNTRY,
				cuntryNmae);
		if (province == null || province == "" || provinceID.equals("310000")
				|| provinceID.equals("110000") || provinceID.equals("120000")
				|| provinceID.equals("500000")) {
			SPUtils.put(ActivityAreaChina.this, null,
					SpPublicParams.PROVINCECITYID, provinceID);
			SPUtils.put(ActivityAreaChina.this, null,
					SpPublicParams.PROVINCECITY, city + "");

		} else {

			SPUtils.put(ActivityAreaChina.this, null,
					SpPublicParams.PROVINCECITY, cityNmae);
			SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.AREA, area
					+ "");
			SPUtils.put(ActivityAreaChina.this, null, SpPublicParams.PROVINCE,
					province);
		}

		System.out.print("wocao" + "1" + "\n" + cuntryNmae + "\n" + province
				+ "\n" + cityNmae + "\n" + city);

		finish();
	}

	/**
	 * 为ListView设置适配器
	 * 
	 * @param list
	 */
	private void setAdapter(List<CityBeans> list) {

		if (list != null) {
			adapter = new ChinaCityAdapter(this, list);
			country_lv_list.setAdapter(adapter);
		} else {
			finish();
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		default:
			break;
		}
	}

}